﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using PointOfSale.Global;
using PointOfSale.Class;
using PointOfSale.Configuration.User;


namespace PointOfSale.POSSystem
{
	/// <summary>
	/// Summary description for ChangePassword.
	/// </summary>
	public class frmChangePassword : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtVerifyPassword;
		private System.Windows.Forms.TextBox txtNewPassword;
		private System.Windows.Forms.TextBox txtOldPassword;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.Button btnOK;
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmChangePassword()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtVerifyPassword = new System.Windows.Forms.TextBox();
			this.txtNewPassword = new System.Windows.Forms.TextBox();
			this.txtOldPassword = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txtVerifyPassword);
			this.groupBox1.Controls.Add(this.txtNewPassword);
			this.groupBox1.Controls.Add(this.txtOldPassword);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 0);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(256, 104);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Chọn mật khẩu";
			this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter);
			// 
			// txtVerifyPassword
			// 
			this.txtVerifyPassword.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.txtVerifyPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtVerifyPassword.Location = new System.Drawing.Point(104, 72);
			this.txtVerifyPassword.Name = "txtVerifyPassword";
			this.txtVerifyPassword.PasswordChar = '*';
			this.txtVerifyPassword.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
			this.txtVerifyPassword.Size = new System.Drawing.Size(144, 20);
			this.txtVerifyPassword.TabIndex = 5;
			this.txtVerifyPassword.Text = "";
			// 
			// txtNewPassword
			// 
			this.txtNewPassword.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.txtNewPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtNewPassword.Location = new System.Drawing.Point(104, 48);
			this.txtNewPassword.Name = "txtNewPassword";
			this.txtNewPassword.PasswordChar = '*';
			this.txtNewPassword.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
			this.txtNewPassword.Size = new System.Drawing.Size(144, 20);
			this.txtNewPassword.TabIndex = 4;
			this.txtNewPassword.Text = "";
			// 
			// txtOldPassword
			// 
			this.txtOldPassword.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.txtOldPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.txtOldPassword.Location = new System.Drawing.Point(104, 24);
			this.txtOldPassword.Name = "txtOldPassword";
			this.txtOldPassword.PasswordChar = '*';
			this.txtOldPassword.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
			this.txtOldPassword.Size = new System.Drawing.Size(144, 20);
			this.txtOldPassword.TabIndex = 3;
			this.txtOldPassword.Text = "";
			// 
			// label3
			// 
			this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label3.Location = new System.Drawing.Point(8, 72);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(88, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "Xác nhận lại";
			// 
			// label2
			// 
			this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label2.Location = new System.Drawing.Point(8, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "Mật khẩu mới";
			// 
			// label1
			// 
			this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Mật khẩu cũ";
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(184, 120);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 1;
			this.btnClose.Text = "Thoát";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnOK
			// 
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOK.Location = new System.Drawing.Point(104, 120);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "Thay đổi";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// frmChangePassword
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(272, 150);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmChangePassword";
			this.Text = "Thay đổi mật khẩu";
			this.Load += new System.EventHandler(this.frmChangePassword_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private string encryptString(string strToEncrypt)
		{
			System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
			byte[] bytes = ue.GetBytes(strToEncrypt);
  
			// encrypt bytes
			System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
			byte[] hashBytes = md5.ComputeHash(bytes);
  
			// Convert the encrypted bytes back to a string (base 16)
			string hashString = "";
  
			for(int i=0;i<hashBytes.Length;i++)
			{
				hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
			}
  
			return hashString.PadLeft(32,'0');
		}

		private bool TestData(int intUserId)
		{
			bool _TestData = true;
			try
			{
				string SelectCommand = "SELECT Password FROM tblUser WHERE UserId = @intUserId";
				SqlCommand _sqlCommand = new SqlCommand(SelectCommand, clsGlobal.g_DataConn);
				_sqlCommand.Parameters.Add("@intUserId", System.Data.SqlDbType.Int, 4);
				_sqlCommand.Parameters["@intUserId"].Value = intUserId;
				SqlDataReader dr = _sqlCommand.ExecuteReader();
				string strPassword = "";
				while(dr.Read())
				{
					strPassword = dr["Password"].ToString();
					if(!this.encryptString(this.txtOldPassword.Text.Trim()).Equals(strPassword))
					{
						_TestData = false;
						MessageBox.Show(clsGlobal.g_Hashtable["msgOldPasswordWrong"].ToString());
						this.txtOldPassword.Clear();
					}
				}
				dr.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			if(!this.txtNewPassword.Text.Trim().Equals(this.txtVerifyPassword.Text.Trim()))
			{
				_TestData = false;
				MessageBox.Show(clsGlobal.g_Hashtable["msgCofirmPassword"].ToString());
				
			}
			return _TestData;

		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if(this.TestData(clsGlobal.g_UserId))
			{
				clsUser user = new clsUser();
				user.PassWord = this.encryptString(this.txtNewPassword.Text.Trim());
				user.UserId = clsGlobal.g_UserId;
				user.ChangePassword();
				MessageBox.Show(clsGlobal.g_Hashtable["msgSucessChage"].ToString());
				this.Close();
			}
		}

		private void frmChangePassword_Load(object sender, System.EventArgs e)
		{
			this.groupBox1.Text = clsGlobal.g_Hashtable["ChangePassordUserName"].ToString() + clsGlobal.g_UserName;
			this.AcceptButton = btnOK;
			this.CancelButton = btnClose;
		}

		private void groupBox1_Enter(object sender, System.EventArgs e)
		{
		
		}

	}
}
